package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.Module;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.RoleService;
import com.hushao.lims.weblims.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Set;

/**
 * @author 930588706
 * @date 18/1/14
 * 主页控制器
 */
@Controller
public class MainController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserUtil userUtil;

    @GetMapping("/")
    public String root(){
        return "redirect:/index";
    }

    @GetMapping("/index")
    public String index(Model model) throws Exception{
        TeacherInfo teacherInfo= (TeacherInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(teacherInfo==null){
           throw new Exception("抱歉！请重新登录");
        }
        Set<Module> modules=roleService.listModule(teacherInfo.getTeacherId());
        model.addAttribute("teacher",teacherInfo);
        model.addAttribute("modules",modules);
        return "index";
    }

    @GetMapping("/login")
    public String login()throws Exception{
        return "login";
    }

    /**
     * 登陆失败
     * @param model
     * @return
     */
    @GetMapping("/login-error")
    public String loginError(Model model){
        model.addAttribute("loginError",true);
        model.addAttribute("errorMsg","登陆失败！");
        return "login";
    }
    @GetMapping("/403")
    public String error(Model model){
        model.addAttribute("error","权限不够");
        return "403";
    }
}
